<?php

namespace App\Models\User;

use App\Models\Common\CommonModel;
use App\Models\Rbac\GuardUser;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Facades\Log;

class LiPei extends CommonModel
{
    use HasFactory;

    protected $primaryKey = 'case_number';
    protected $keyType = 'string';
    protected $table = 'li_pei_order';

    public function getGuardUser() {
        return $this->hasOne(GuardUser::class,'id','account')->withDefault(['name'=>'未知']);
    }

    public function getRecords(array $conditions) {
        $records = $this->newQuery();
        if(!empty($conditions['baoXianName'])){
            $records = $records->where('baoxian_name','like','%'.$conditions['baoXianName'].'%');
        }
        if(!empty($conditions['importNumber'])){
            $records = $records->where('import_number',$conditions['importNumber']);
        }
        if(!empty($conditions['caseNumber'])){
            $records = $records->where('case_number',$conditions['caseNumber']);
        }
        if(!empty($conditions['carNumber'])){
            $records = $records->where('car_number',$conditions['carNumber']);
        }
        Log::info('con',$conditions);
        if(!empty($conditions['dingSunPrice'])){
            if(is_array($conditions['dingSunPrice'])){
                $conditions['dingSunPrice'] = array_filter($conditions['dingSunPrice']);
                if(count($conditions['dingSunPrice'])>0){
                    $records->whereBetween('loss_price',$conditions['dingSunPrice']);
                }
            }
            else{
                $records = $records->where('loss_price',$conditions['dingSunPrice']);
            }
        }
        if(!empty($conditions['chuXianDate'])){
            $records = $records->whereBetween('chu_xian_date',$conditions['chuXianDate']);
        }
        if(!empty($conditions['createdAt'])){
            $records = $records->whereBetween('created_at',$conditions['createdAt']);
        }
        return $records;
    }


}
